<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { getAccompanyDetail, joinAccompany } from '@/api/accompany'
import { showConfirmDialog, showToast } from 'vant'

const route = useRoute()
const router = useRouter()
const detail = ref(null)
const loading = ref(true)

const loadDetail = async () => {
  try {
    loading.value = true
    const res = await getAccompanyDetail(route.params.id)
    detail.value = res.data
  } catch (error) {
    showToast('获取详情失败')
  } finally {
    loading.value = false
  }
}

const handleJoin = (id) => {
  showConfirmDialog({
    title: '申请加入',
    message: '确定要申请加入该结伴吗？',
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    confirmButtonColor: '#3eaf7c'
  })
    .then(async () => {
      try {
        await joinAccompany(id)
        showToast({
          message: '申请成功',
          icon: 'success',
          className: 'custom-toast'
        })
      } catch (error) {
        showToast({
          message: '申请失败',
          icon: 'cross',
          className: 'custom-toast error'
        })
      }
    })
    .catch(() => {})
}

onMounted(() => {
  loadDetail()
})
</script>

<template>
  <div class="accompany-detail">
    <van-nav-bar 
      title="结伴详情" 
      left-arrow 
      @click-left="router.back()"
      fixed
      placeholder
    />
    
    <van-skeleton title :row="10" :loading="loading" v-if="loading" />
    
    <template v-else>
      <div class="detail-card">
        <div class="header">
          <div class="title">{{ detail.title }}</div>
          <div class="status" :class="detail.status === 0 ? 'recruiting' : 'closed'">
            {{ detail.status === 0 ? '招募中' : '已结束' }}
          </div>
        </div>

        <div class="creator-info">
          <img :src="detail.createUser.avatar" alt="creator avatar">
          <div class="creator-detail">
            <span class="name">{{ detail.createUser.username }}&nbsp;</span>
            <span class="email">联系方式:&nbsp;{{ detail.createUser.email }}</span>
          </div>
        </div>

        <div class="info-section">
          <div class="info-item">
            <van-icon name="location" color="#ff6b81" />
            <span>目的地：{{ detail.destination }}</span>
          </div>
          <div class="info-item">
            <van-icon name="friends" color="#4834d4" />
            <span>人数：{{ detail.currentMembers }}/{{ detail.maxMembers }}人</span>
          </div>
          <div class="info-item">
            <van-icon name="clock" color="#2d98da" />
            <span>时间：{{ detail.startTime.split('T')[0] }} 至 {{ detail.endTime.split('T')[0] }}</span>
          </div>
        </div>

        <div class="content-section">
          <h3>详细说明</h3>
          <p>{{ detail.content }}</p>
        </div>

        <div class="members-section">
          <h3>当前成员 ({{ detail.user.length }}人)</h3>
          <div class="members-list">
            <div v-for="member in detail.user" :key="member.id" class="member-item">
              <img :src="member.avatar" alt="avatar">
              <span class="name">{{ member.username }}</span>
              <span class="uid">ID: {{ member.uid }}</span>
            </div>
          </div>
        </div>
      </div>

      <div class="action-bar" v-if="detail.status === 0">
        <van-button 
          type="primary" 
          block 
          round
          class="join-btn"
          @click="handleJoin(detail.id)"
        >
          申请加入
        </van-button>
      </div>
    </template>
  </div>
</template>

<style scoped lang="scss">
.accompany-detail {
  min-height: 100vh;
  background: linear-gradient(to bottom, #f6f7f9, #e9ebee);
  padding-bottom: 80px;

  .detail-card {
    margin: 16px;
    background: #fff;
    border-radius: 16px;
    padding: 20px;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.06);
    animation: slideIn 0.5s ease-out;

    .header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 20px;

      .title {
        font-size: 20px;
        font-weight: 600;
        color: #2c3e50;
      }

      .status {
        padding: 4px 12px;
        border-radius: 20px;
        font-size: 13px;
        
        &.recruiting {
          background: rgba(62, 175, 124, 0.1);
          color: #3eaf7c;
        }
        
        &.closed {
          background: rgba(245, 108, 108, 0.1);
          color: #f56c6c;
        }
      }
    }

    .creator-info {
      display: flex;
      align-items: center;
      gap: 16px;
      margin-bottom: 20px;

      img {
        width: 40px;
        height: 40px;
        border-radius: 50%;
        object-fit: cover;
        border: 2px solid #fff;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
      }

      .creator-detail {
        .name {
          font-size: 16px;
          font-weight: 600;
          color: #2c3e50;
          margin-bottom: 4px;
        }

        .email {
          font-size: 14px;
          color: #666;
        }
      }
    }

    .info-section {
      background: #f8f9fa;
      border-radius: 12px;
      padding: 16px;
      margin-bottom: 24px;

      .info-item {
        display: flex;
        align-items: center;
        gap: 8px;
        margin-bottom: 12px;
        font-size: 14px;
        color: #666;

        &:last-child {
          margin-bottom: 0;
        }

        .van-icon {
          font-size: 18px;
        }
      }
    }

    .content-section {
      margin-bottom: 24px;

      h3 {
        font-size: 16px;
        font-weight: 600;
        color: #2c3e50;
        margin-bottom: 12px;
      }

      p {
        font-size: 14px;
        color: #666;
        line-height: 1.6;
      }
    }

    .members-section {
      h3 {
        font-size: 16px;
        font-weight: 600;
        color: #2c3e50;
        margin-bottom: 16px;
      }

      .members-list {
        display: flex;
        flex-wrap: wrap;
        gap: 16px;

        .member-item {
          display: flex;
          flex-direction: column;
          align-items: center;
          gap: 8px;

          img {
            width: 50px;
            height: 50px;
            border-radius: 50%;
            object-fit: cover;
            border: 2px solid #fff;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
          }

          span {
            font-size: 13px;
            color: #666;
          }
        }
      }
    }
  }

  .action-bar {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    padding: 16px;
    background: #fff;
    box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.05);

    .join-btn {
      background: linear-gradient(135deg, #42b983 0%, #3eaf7c 100%);
      border: none;
      height: 44px;
      font-size: 16px;
      font-weight: 500;
      box-shadow: 0 4px 12px rgba(62, 175, 124, 0.2);
      
      &:active {
        transform: scale(0.98);
      }
    }
  }
}

@keyframes slideIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
</style> 